<?php
/**
 * 投手管理
 */
namespace app\cps\controller;

use think\facade\Request;
use app\common\Error;
use app\common\Common;
use app\cps\service\PitcherService;

class Pitcher extends Base
{
    private $service;

    public function __construct(PitcherService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    // 渲染列表页
    public function index()
    {
        return view();
    }

    // 渲染详情页
    public function info()
    {
        return view();
    }

    // 添加投手
    public function add()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Pitcher::class)->scene('add')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editPitcher($params, $this->getUserInfo());

        if (!$result || !empty($result['message'])) {
            $message = !empty($result['message']) ? $result['message'] : Error::FAIL_MESSAGE;
            return Common::fail($message);
        }

        return Common::success();
    }

    // 编辑投手
    public function edit()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Pitcher::class)->scene('edit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editPitcher($params,$this->getUserInfo());

        if (!$result) {
            return Common::fail(Error::FAIL_MESSAGE);
        }

        return Common::success();
    }

    // 投手列表
    public function list()
    {
        $params = Request::param();

        $data = $this->service->getPitcherList($params, $this->getUserInfo());

        return Common::success($data);
    }

    // 投手详情
    public function detail()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Pitcher::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getPitcherDetail($params['id']);

        return Common::success($data);
    }

    // 投手下拉列表
    public function select()
    {
        $params = Request::param();
        $data = $this->service->getSelectList($params, $this->getUserInfo());
        return Common::success($data);
    }

    // 分销商用投手下拉列表
    public function selectList()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Order::class)->scene('distribution_select_list')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getSelectList($params);

        if (!empty($data['message'])) {
            return Common::fail($data['message']);
        }

        return Common::success($data);
    }

    // 回传设置
    public function backSetting()
    {
        $params = Request::param();
        $data = $this->service->backSetting($params, $this->getUserInfo());

        if (!empty($data['message'])) {
            return Common::fail($data['message']);
        }
        return Common::success();
    }

    // 获取回传设置
    public function getBackSetSetting()
    {
        $params = Request::param();
        $data = $this->service->getBackSetSetting($params, $this->getUserInfo());
        return Common::success($data);
    }

    // 渲染回传设置页面
    public function backSetInfo()
    {
        return view();
    }
}